package ws;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.util.Log;

public class ResultadoWS {
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;

	/*
	 * 
	 */
	public void coneccionws(){

		boolean londres = true;

		if(londres){
			NAMESPACE = "http://ws/"; 
			URL = "http://172.22.4.108:8080/Libro2SOA/services/ResultadoSOA?wsdl";

			//	Toast.makeText(null, "servidor", Toast.LENGTH_SHORT).show();
			//		Toast.makeText(getBaseContext(),respuesta, 
			//				Toast.LENGTH_SHORT).show();
		}else{
			NAMESPACE = "http://ws/";
			URL = "http://172.22.4.108:8080/Libro2SOA/services/ResultadoSOA?wsdl";
		}
	}
	
	public String addResultado(String resultado,int idPregunta, int idLibro){

		coneccionws();

		METHOD_NAME = "addresultado";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo addresultado = new PropertyInfo();
		PropertyInfo addidPregunta = new PropertyInfo();
		PropertyInfo addidLibro = new PropertyInfo();

		
		addresultado.setName("resultado");
		addresultado.setValue(resultado);
		addresultado.setType(String.class);

		addidPregunta.setName("idPregunta");
		addidPregunta.setValue(idPregunta);
		addidPregunta.setType(String.class);

		addidLibro.setName("idLibro");
		addidLibro.setValue(idLibro);
		addidLibro.setType(String.class);

		
		request.addProperty(addresultado);
		request.addProperty(addidPregunta);
		request.addProperty(addidLibro);
		
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);

		String datosJsonString;
		try
		{ //realiza la llamada
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}
	
	public String findresultado(int idLibro , int idPregunta){
		coneccionws();

		METHOD_NAME = "findresultados";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findlibro = new PropertyInfo();
		PropertyInfo findpregunta = new PropertyInfo();
        
		findlibro.setName("idLibro");
		findlibro.setValue(idLibro);
		findlibro.setType(String.class);
		
		findpregunta.setName("idPregunta");
		findpregunta.setValue(idPregunta);
		findpregunta.setType(String.class);

		request.addProperty(findlibro);
		request.addProperty(findpregunta);
		//Log.i("log-android","ha llegado aqui ws--"+findempresa);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);


		String datosJsonString;
		try
		{

			aht.call("", soapEnvelope);
			Log.i("log-android","ha llegado aquiiii probando--"+URL);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

			datosJsonString =resultString.toString();

			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}


	public String findpreguntas(){
		coneccionws();

		METHOD_NAME = "findpregunta";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//		PropertyInfo findempresa = new PropertyInfo();
//		findempresa.setType(String.class);
//		request.addProperty(findempresa);
		//Log.i("log-android","ha llegado aqui ws--"+findempresa);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);


		String datosJsonString;
		try
		{

			aht.call("", soapEnvelope);
			Log.i("log-android","ha llegado aquiiii probando--"+URL);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

			datosJsonString =resultString.toString();

			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}
	
}

